//
//  HPPlatformView.swift
//  iPhone
//
//  Created by Toj on 7/20/23.
//

import UIKit

protocol HPPlatformViewDelegate: NSObjectProtocol {
    func platformViewDidTapMobile(_ platformView: HPPlatformView)
    func platformViewDidTapTV(_ platformView: HPPlatformView)
}
 
extension HPPlatformViewDelegate {
    func platformViewDidTapMobile(_ platformView: HPPlatformView) { }
    func platformViewDidTapTV(_ platformView: HPPlatformView) { }
}

class HPPlatformView: UIView {
    
    var selectedTV = false { didSet {
        
        guard selectedTV else {
            selectedButton = mobileButton
            delegate?.platformViewDidTapMobile(self)
            return
        }
        selectedButton = tvButton
        delegate?.platformViewDidTapTV(self)
    } }
    
    weak var delegate: HPPlatformViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
        setupMakeInitialize()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupMakeLayoutSubviews()
    }
    
    /* 圆角 */
    private let cornerHeight: CGFloat = 22
    private lazy var cornerView: UIView = {
        let view = UIView()
        view.backgroundColor = "#FFE08F".to_color
        view.cm_cornerRadius = cornerHeight * 0.5
        return view
    }()
    
    private lazy var animateView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.cm_cornerRadius = (cornerHeight - 2) * 0.5
        return view
    }()

    /* Mobile VIP */
    private(set) lazy var mobileButton: UIButton = {
        let view = setupButton()
        view.setTitle("membership_Mobile".localized(), for: .normal)
        view.addTarget(self, action: #selector(didTapMobile), for: .touchUpInside)
        return view
    }()
    
    /* TV VIP */
    private(set) lazy var tvButton: UIButton = {
        let view = setupButton()
        view.setTitle("membership_TV".localized(), for: .normal)
        view.addTarget(self, action: #selector(didTapTV), for: .touchUpInside)
        return view
    }()
    
    private func setupButton() -> UIButton {
        let view = UIButton()
        view.titleLabel?.font = 14.to_boldFont
        view.setTitleColor("#333333".to_color, for: .normal)
        view.setTitleColor("#99773C".to_color, for: .selected)
        return view
    }
    
    private let margin: CGFloat = 10
    
    private var selectedButton: UIButton? { willSet {
        selectedButton?.isSelected = false
        newValue?.isSelected = true
        
        guard let selView = newValue else { return }
        var animateFrame = selView.frame
        animateFrame.size.height = cornerHeight - 2
        animateFrame.size.width = animateFrame.width - 2
        UIView.animate(withDuration: 0.2) {
            self.animateView.frame = animateFrame
            self.animateView.center = selView.center
        }
    } }
}

// MARK: - 点击事件/ 通知事件回调
@objc private extension HPPlatformView {
    
    func didTapMobile() {
        selectedButton = mobileButton
        delegate?.platformViewDidTapMobile(self)
    }
    
    func didTapTV() {
        selectedButton = tvButton
        delegate?.platformViewDidTapTV(self)
    }
}

// MARK: - Public
extension HPPlatformView {
    
    func platformWidth(isTV: Bool) -> CGFloat {
        guard isTV else { return 0 }
        var pwidth = mobileButton.frame.width
        pwidth += tvButton.frame.width
        return pwidth
    }
    
    func setupPlatform(isTV: Bool) {
        isHidden = !isTV
    }
}

// MARK: - Private
private extension HPPlatformView {
    
    func makeupWidth(_ width: CGFloat) -> CGFloat {
        guard width > 0 else { return 0 }
        return 16 + width // 左右间距16: 2 * 8
    }
}

// MARK: - 初始化设置
private extension HPPlatformView {
    
    func setupMakeAddSubviews() {
        addSubview(cornerView)
        addSubview(animateView)
        addSubview(mobileButton)
        addSubview(tvButton)
    }
    
    func setupMakeLayoutSubviews() {
        cornerView.snp.makeConstraints { make in
            make.left.right.centerY.equalTo(self)
            make.height.equalTo(cornerHeight)
        }
        layoutIfNeeded()
        
        mobileButton.titleLabel?.sizeToFit()
        var vWidth = mobileButton.titleLabel?.frame.width ?? 0
        let mobileWidth = makeupWidth(vWidth)
        
        tvButton.titleLabel?.sizeToFit()
        vWidth = tvButton.titleLabel?.frame.width ?? 0
        let tvWidth = makeupWidth(vWidth)
        
        let sheight = frame.height
        let height: CGFloat = 44
        let y = (sheight - height) * 0.5
        
        mobileButton.frame = CGRect(x: 0, y: y, width: mobileWidth, height: height)
        tvButton.frame = CGRect(x: mobileWidth, y: y, width: tvWidth, height: height)
    }
    
    func setupMakeInitialize() {
    }
}
